/**
 * Created by John on 2016/4/11.
 */
var express=require("express");
var _=require("underscore");
var mongoose = require('mongoose');
var Movie=require("./models/movie");
mongoose.connect("mongodb://localhost/web");
var port=process.env.PORT||3000;
var app=express();
app.set('views','./views/pages/');
app.set('views engine', 'jade');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
// parse application/json
app.use(bodyParser.json());
var path=require('path');
app.use(express.static(path.join(__dirname,'public')));
app.locals.moment=require("moment");
//index page
app.get("/",function(req,res){
    Movie.fetch(function(err,movies){
        if(err){
            console.log(err);
        }else{
            res.render("index",{
                title: '首页',
                /*movies: [{
                 title:'太阳的后裔',
                 _id: 1,
                 poster: 'http://r3.ykimg.com/05160000530EEB63675839160D0B79D5'
                 },{
                 title:'太阳的后裔',
                 _id: 2,
                 poster: 'http://r3.ykimg.com/05160000530EEB63675839160D0B79D5'
                 },{
                 title:'太阳的后裔',
                 _id: 3,
                 poster: 'http://r3.ykimg.com/05160000530EEB63675839160D0B79D5'
                 },{
                 title:'太阳的后裔',
                 _id: 4,
                 poster: 'http://r3.ykimg.com/05160000530EEB63675839160D0B79D5'
                 },{
                 title:'太阳的后裔',
                 _id: 5,
                 poster: 'http://r3.ykimg.com/05160000530EEB63675839160D0B79D5'
                 }]*/
                movies:movies
            })
        }
    });
});
//list page
app.get("/admin/list",function(req,res){
    Movie.fetch(function(err,movies){
        if(err){
            console.log(err);
        }else{
            res.render("list",{
                title: '列表页',
                /*movies:[{
                 _id:1,
                 doctor:'javan',
                 country:'china',
                 title:'钢铁侠',
                 year:2014,
                 poster:'http://r3.ykimg.com/05160000530EEB63675839160D0B79D5',
                 language:'chinese',
                 flash:'http://player.youku.com/player.php/sid/XNjA1Njc0NTUy/v.swf',
                 summary:'中国制造中国制造中国制造中国制造中国制造中国制造中国制造中国制造中国制造'
                 },{
                 _id:2,
                 doctor:'javan',
                 country:'china',
                 title:'钢铁侠',
                 year:2014,
                 poster:'http://r3.ykimg.com/05160000530EEB63675839160D0B79D5',
                 language:'chinese',
                 flash:'http://player.youku.com/player.php/sid/XNjA1Njc0NTUy/v.swf',
                 summary:'中国制造中国制造中国制造中国制造中国制造中国制造中国制造中国制造中国制造'
                 }]*/
                movies:movies
            })
        }
    });
});
//detail page
app.get("/movie/:id",function(req,res){
    var id=req.params.id;
    Movie.findById(id,function(err,movie){
        if(err){
            console.log(err)
        }else{
            res.render("detail",{
                title: '详情页'+movie.title,
                /*movie:{
                 doctor:'javan',
                 country:'china',
                 title:'钢铁侠',
                 year:2014,
                 //poster:'http://r3.ykimg.com/05160000530EEB63675839160D0B79D5',
                 language:'chinese',
                 flash:'http://player.youku.com/player.php/sid/XNjA1Njc0NTUy/v.swf',
                 summary:'中国制造中国制造中国制造中国制造中国制造中国制造中国制造中国制造中国制造'
                 }*/
                movie:movie
             })
        }
    })
});
//admin page
app.get("/admin/movie",function(req,res){
    res.render("admin",{
        title: '管理页',
        movie:{
            doctor:'',
            country:'',
            title:'',
            year:'',
            poster:'',
            language:'',
            flash:'',
            summary:''
        }
    })
});
//admin post movie
app.post("/admin/movie/new",function(req,res){
    console.log("1");
    var id=req.body.movie._id;
    var movieObj=req.body.movie;
    var _movie;
    console.log("id="+id);
    if(id!==undefined){
        Movie.findById(id,function(err,movie){
            if(err){
                console.log(err);
            }else{
                console.log("here")
               _movie= _.extend(movie,movieObj);
               _movie.save(function(err,movie){
                   if(err){
                       console.log(err)
                   }else{
                       res.redirect("/movie/"+movie._id);
                   }
               })
            }
        })
    }else{
        _movie=new Movie({
            doctor:movieObj.doctor,
            title:movieObj.title,
            country:movieObj.country,
            year:movieObj.year,
            poster:movieObj.poster,
            language:movieObj.language,
            flash:movieObj.flash,
            summary:movieObj.summary
        });
        _movie.save(function(err,movie){
            if(err){
                console.log(err)
            }else{
                res.redirect("/movie/"+movie._id);
            }
        })
    }
});
//admin update movie
app.get("/admin/update/:id",function(req,res){
      var id=req.params.id;
    if(id){
        Movie.findById(id,function(err,movie){
            if(err){
                console.log(err)
            }else{
                res.render("admin",{
                   title:"后台更新页",
                   movie:movie
                })
            }
        })
    }
});
//list delete
app.delete("/admin/list",function(req,res){
   var id=req.query.id;
    if(id){
        Movie.remove({_id:id},function(err,movie){
            if(err){
                console.log(err)
            }else{
                res.json({success:1})
            }
        })
    }
});
app.listen(port);
console.log("listen port "+port);